/*
 ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1

 * The contents of this file are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Blazing Games Inc. (Billy D. Spelchan) code.
 *
 * The Initial Developer of the Original Code is
 * Blazing Games Inc.
 * Portions created by the Initial Developer are Copyright (C) 1999, 2003, 2005-2006
 * the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s):
 *
 ***** END LICENSE BLOCK *****
 */
package com.blazinggames.holeman;

import java.applet.*;
import java.awt.*;
//import java.awt.image.*;
import java.awt.event.*;
import java.io.*;

public class TitleScreen
extends Panel
implements MouseListener, KeyListener {
// *** CONSTANTS ***
  private final static Font fontSOUND = new Font("TimesRoman", Font.PLAIN, 18);

  private final static String sSOUNDON = "Sound Enabled!  Press T to Toggle.";
  private final static String sSOUNDOFF = "Sound Disabled!  Press T to Toggle.";

// *** VARIABLES ***
  Image iTitle;

  Map map;
	ActionListener eventActionListener;

  boolean bKeyDown;

  
// *** CONSTRUCTORS ***
	public TitleScreen(Map m, Applet a, ActionListener al, boolean bJar) {
//    int cntr;

    map = m;
    Toolkit tk = Toolkit.getDefaultToolkit();

    if (bJar) {
      InputStream instreamImage = a.getClass().getResourceAsStream("title.gif");
      if (instreamImage == null) {
        System.err.println("Can not get image from JAR! - title");
        return;
      }
      try {
        byte imageBytes[] = new byte[instreamImage.available()];
        instreamImage.read(imageBytes);
        instreamImage.close();
        iTitle = tk.createImage(imageBytes);
      } catch (IOException e) {
        System.err.println("Can not get image from JAR!- title");
      }
    }
    else {
        if (a != null)
        {
            iTitle = a.getImage(a.getCodeBase(), "res/holeman/title.gif");
        }
        else
        {
            System.out.println("Loading title with toolkit");
            iTitle = tk.getImage("res/holeman/title.gif");            
        }
    }

    // Use the zone adapter as the mouse listeners
		addMouseListener(this);
    addKeyListener(this);
    eventActionListener = al;
    map.bSoundEnabled = bKeyDown = false;
	}

  // Displays the contents of the panel.  Most work handled by zone adapter
	public void paint(Graphics g) {
    requestFocus();
    g.drawImage(iTitle, 0, 0, 640,480, this);

    Color oldColor = g.getColor();
    Font oldFont = g.getFont();
    FontMetrics fm;

    g.setColor(Color.green);
    g.setFont(fontSOUND);
    fm = g.getFontMetrics();

    if (map.bSoundEnabled)
      g.drawString(sSOUNDON, 320-(fm.stringWidth(sSOUNDON)>>1), 450);
    else
      g.drawString(sSOUNDOFF, 320-(fm.stringWidth(sSOUNDOFF)>>1), 450);

    g.setColor(oldColor);
    g.setFont(oldFont);
	}

// *** MOUSE EVENTS ***
  /**
    Mouse Clicked event from MouseListener.
    Invoked when the mouse has been clicked on a component.
  */
  public void mouseClicked(MouseEvent e) { /* ignore */ }

  /**
    mousePressed event from MouseListener.
    Invoked when a mouse button has been pressed on a component.
  */
  public void mousePressed(MouseEvent e) {
  }

 /**
    mouseReleased event from MouseListener.
    Invoked when a mouse button has been released on a component.
  */
  public void mouseReleased(MouseEvent e) {
      eventActionListener.actionPerformed(
            new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Speed"));
  }

  /**
    mouseEntered event from MouseListener.
    Invoked when the mouse enters a component.
  */
  public void mouseEntered(MouseEvent e) { /* ignore */ }

 /**
    mouseExited event from MouseListener.
    Invoked when the mouse exits a component.
  */
  public void mouseExited(MouseEvent e) { /* ignore */ }

  /**
   * Invoked when a key has been typed.
   * This event occurs when a key press is followed by a key release.
   */
  public void keyTyped(KeyEvent e) {/* IGNORED */}

  /**
   * Invoked when a key has been pressed.
   */
  public void keyPressed(KeyEvent e) {
    bKeyDown = true;
  }

  /**
   * Invoked when a key has been released.
   */
  public void keyReleased(KeyEvent e) {
    if (bKeyDown) {
      bKeyDown = false;
      if (e.getKeyCode() == KeyEvent.VK_T) {
        map.bSoundEnabled = !map.bSoundEnabled;
        repaint();
      }
      else
        eventActionListener.actionPerformed(
              new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Speed"));
    }

  }


}
